package ro.flashbite.game_engine.view.sheet.cpu
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.PixelSnapping;
	import flash.geom.Rectangle;
	
	import ro.flashbite.errors.ArgumentIncompatibleError;
	
	/**
	 * Base class that extends SimpleSpriteSheetCPU, but is uses instead vectors of BitmapData and XML to draw visuals with extra speed.
	 * Can only play frame bases animation; No FrameLabels are recognized
	 *  
	 * v 1.0
	 * 
	 * @author Adrian Barbu
	 */
	public final class ComplexSpriteSheetCPU extends SimpleSpriteSheetCPU
	{
		private var _complexSheetsBmpData		:	Vector.<BitmapData>;
		
		// ================================================================================================================================
		// CONSTRUCTOR
		// ================================================================================================================================
		
		public function ComplexSpriteSheetCPU(sheetBmpData:Vector.<BitmapData>, settingsXMLs:Vector.<XML>)
		{
			super(null, null);
			
			_constructor(sheetBmpData, settingsXMLs);
		}
		private function _constructor(sheetBmpData:Vector.<BitmapData>, settingsXMLs:Vector.<XML>):void
		{
			if (sheetBmpData == null) throw new ArgumentIncompatibleError("sheetBmpData");
			if (settingsXMLs == null) throw new ArgumentIncompatibleError("settingsXMLs");
			
			var len1:uint = sheetBmpData.length,
				len2:uint = settingsXMLs.length;
			if (len1 != len2) throw new Error("sheetBmpData.length() != settingsXMLs.length()");
			
			//create vectors
			_complexSheetsBmpData = new Vector.<BitmapData>();
			_rectangles = new Vector.<Rectangle>();
			
			var indexRect:int = 0,
				i:int,
				currentBmpData:BitmapData,
				currentXML:XML,
				xmlListSlices:XMLList,
				xmlSlice:XML,
				rect:Rectangle;
			for (i = 0; i < len1; i++) {
				//set locations
				currentBmpData = sheetBmpData[i] as BitmapData;
				currentXML = settingsXMLs[i] as XML;
				//read in each xml + save rectangles and bitmapDatas in
				xmlListSlices = currentXML.slices.slice as XMLList;
				for each (xmlSlice in xmlListSlices) {
					rect = new Rectangle(Number(xmlSlice.@x), Number(xmlSlice.@y), Number(xmlSlice.@width), Number(xmlSlice.@height));
					_rectangles.push(rect);
					_complexSheetsBmpData.push(currentBmpData);
				}
			}
			
			//set total frames
			_totalFrames = _rectangles.length;
			
			//create globals
			_visualBmpData = new BitmapData(rect.width, rect.height, true, 0x00FFFFFF);
			_visual = new Bitmap(_visualBmpData, PixelSnapping.NEVER, true);
			
			//add bmp to this
			this.addChild(_visual);
			
			//stop it on frame 1
			gotoAndStop(1);
			
			//disable interaction
			disable();
		}
		
		// ================================================================================================================================
		// ISleepWakeupDestroy
		// ================================================================================================================================
		
		override public function destroy():void
		{
			_complexSheetsBmpData = null;
			
			super.destroy();
		}
		
		// ================================================================================================================================
		// PROTECTED OVERRIDES
		// ================================================================================================================================
		
		override protected function createBlittValues():void
		{
			//_currentFrame is 1 based, _rectangles and _currentBlittedBmpData are 0 based
			_currentBlittedRectangle = _rectangles[_currentFrame - 1]; //super.createBlittValues(); //no call to super.createBlittValues() because of speed issues
			_currentBlittedBmpData = _complexSheetsBmpData[_currentFrame - 1];
		}
	}
}